var model = require('../model');

var Serie = model.Serie;

//# 套系信息
//
exports.index = function(req, res, next) {
    res.render('backend/serie/index', {
        title: '套系信息',
    });
};


//# 套系列表
//
exports.list = function(req, res, next) {
    var pageInfo = {};

    pageInfo.pageIndex = parseInt(req.query.pageIndex) || 1;
    pageInfo.pageSize = parseInt(req.query.pageSize) || 10;
    var skipIndex = (pageInfo.pageIndex - 1) * (pageInfo.pageSize);

    var titleReg = new RegExp(req.query.title);

    var query = {
        title: titleReg,
        deleted: false
    };

    Serie.count(query, function(error, count) {
        if (error) {
            console.log(error);
        } else {
            pageInfo.count = count;
            pageInfo.totalPage = Math.ceil(count / pageInfo.pageSize);
        }
    });

    Serie.find(query)
        .skip(skipIndex)
        .limit(pageInfo.pageSize)
        .exec(function(error, series) {
            if (error) {
                console.log(error);
                return;
            }
            res.render('backend/serie/list', {
                series: series,
                pageInfo: pageInfo
            });
        });
};

//# 录入套系信息
//
exports.new = function(req, res, next) {
    res.render('backend/serie/new', {
        title: '增加套系',
    });
};

//# 保存新套系信息
//
exports.create = function(req, res, next) {
    console.log(req.body);
    var serieObj = {
        title: req.body.title,
        price: req.body.price,
        optCount: req.body.optCount,
        takeCount: req.body.takeCount,
        intro: req.body.intro,
        pictures: req.body.pictures,
        createAt: new Date(),
        deleted: false
    }
    var serie = new Serie(serieObj);

    serie.save(function(error) {
        var result = {};
        if (error) {
            result.success = false;
            res.json(result);
        } else {
            result.success = true;
            res.append('Location', '/backend/series');
            res.json(result);
        }
    });
};

//# 编辑员工界面
//
exports.edit = function(req, res, next) {
    var serieId = req.params.id;
    Serie.findOne({
        _id: serieId
    }, function(error, serie) {
        if (error) {
            console.log(error);
        } else {
            res.render('backend/serie/edit', {
                title: '编辑套系',
                serie: serie
            });
        }
    });
};

//# 更新套系信息
//
exports.update = function(req, res, next) {

    var serieId = req.params.id;

    var updateSerie = {
        title: req.body.title,
        price: req.body.price,
        optCount: req.body.optCount,
        takeCount: req.body.takeCount,
        intro: req.body.intro,
        pictures: req.body.pictures,
        updateAt: new Date()
    };

    console.log(updateSerie);

    Serie.update({
        _id: serieId
    }, updateSerie, function(error) {
        var result = {};
        if (error) {
            result.success = false;
            res.json(result);
        } else {
            result.success = true;
            res.json(result);
        }
    });
};

//# 删除套系信息
//
exports.delete = function(req, res, next) {
    var serieId = req.params.id;

    Serie.update({
        _id: serieId
    }, {
        deleted: true
    }, function(error) {
        var result = {};
        if (error) {
            result.success = false;
            res.json(result);
        } else {
            result.success = true;
            res.json(result);
        }
    });
};

//# 样图管理
//
exports.pictureManage = function(req, res, next) {
    var file = req.files[0];

    var result = {
        success: true,
        filename: file.filename,
        originalname: file.originalname
    };
    res.json(result);

};